package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.QuestionTypeEntity;
import com.tfjybj.itoo.exam.model.QuestionTypeIdModel;
import com.tfjybj.itoo.exam.model.QuestionTypeModel;
import com.tfjybj.itoo.exam.provider.service.QuestionTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * QuestionTypeController
 * questionType表
 *
 * @author 郭凯
 * @version 2.0.0
 * @since 2.0.0 2018-11-21 21:10:06
 */
@Api(tags = {"questionType表接口"})
@RequestMapping(value = "/questionType")
@RestController
public class QuestionTypeController {

    @Resource
    private QuestionTypeService questionTypeService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model QuestionTypeModel
     * @return 添加的结果
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody QuestionTypeModel model) {
        if (StringUtils.isEmpty(model.getQuestionTypeName())) {
            return ItooResult.build(ItooResult.FAIL, "questionTypeName为空");
        }
        if (StringUtils.isEmpty(model.getScoreType())) {
            return ItooResult.build(ItooResult.FAIL, "scoreType为空");
        }
        QuestionTypeEntity questionTypeEntity = new QuestionTypeEntity();
        BeanUtils.copyProperties(model, questionTypeEntity);

        questionTypeService.save(questionTypeEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        questionTypeService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        questionTypeService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model QuestionTypeModel
     * @return 修改后的结果
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据id修改questionType")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody QuestionTypeModel model) {
        if (StringUtils.isEmpty(model.getQuestionTypeName())) {
            return ItooResult.build(ItooResult.FAIL, "questionTypeName为空");
        }
        if (StringUtils.isEmpty(model.getScoreType())) {
            return ItooResult.build(ItooResult.FAIL, "scoreType为空");
        }
        QuestionTypeEntity questionTypeEntity = new QuestionTypeEntity();
        BeanUtils.copyProperties(model, questionTypeEntity);
        questionTypeService.updateById(questionTypeEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * @param id 主键id
     * @return 根据id查找QuestionType     *
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        QuestionTypeEntity questionTypeEntity = questionTypeService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionTypeEntity);
    }

    /**
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 分页查询所有QuestionType     *
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "分页查询所有QuestionType")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<QuestionTypeEntity> questionTypes = questionTypeService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionTypes);
    }

    /**
     * 根据题型名称模糊查询questionType
     *
     * @param questionTypeName 题型名称
     * @param pageNo           页码
     * @param pageSize         页数
     * @return 模糊查询的questionType
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据题型名称模糊查询questionType", notes = "分页根据题型名称模糊查询questionType")
    @GetMapping(value = "queryQuestionTypeByLikeQuestionTypeName/{pageNo}/{pageSize}")
    public ItooResult queryQuestionTypeByLikeQuestionTypeName(@RequestParam(required = false, defaultValue = "") String questionTypeName,
                                                              @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                              @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<QuestionTypeEntity> questionTypeList = questionTypeService.queryByLikeQuestionTypeName(questionTypeName, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionTypeList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 查询题型信息
     *
     * @return 题型信息
     * @author 郭满亮
     * @since 2.0.0 2018年12月6日11:30:52
     */
    @ApiOperation(value = "查询题型信息")
    @GetMapping(value = "/queryAllQuestionType")
    public ItooResult queryAllQuestionType() {
        List<QuestionTypeModel> questionTypeEntityList = questionTypeService.queryAllQuestionType();
        if (questionTypeEntityList.size() > 0) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionTypeEntityList);
        } else {
            return ItooResult.build(ItooResult.FAIL, "暂无数据", null);
        }

    }

    /**
     * 根据题型code查询题型id
     *
     * @return 题型id
     * @author 郭凯
     * @since 2.0.0 2019-2-21 15:43:28
     */
    @ApiOperation(value = "根据code查询题型id")
    @GetMapping(value = "/queryByCode/{code}")
    public ItooResult queryByCode(@ApiParam(value = "题型code", required = true) @PathVariable String code) {
        String questionTypeId = questionTypeService.queryByCode(code);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionTypeId);
    }


    /**
     * 课程练习-根据课程id查询题型
     *
     * @param courseId
     * @return 是否查询成功
     * @author 贾建清
     * @since 2.0.0 2019年3月24日10:37:00
     */
    @ApiOperation(value = "课程练习-根据课程id查询题型")
    @GetMapping(value = "/queryQuestionType/{courseId}")
    public ItooResult queryQuestionType(@ApiParam(value = "courseId", required = true) @PathVariable String courseId) {
        if (StringUtils.isEmpty(courseId)) {
            return ItooResult.build(ItooResult.FAIL, "传递courseId不可以为空", null);
        }
        List<QuestionTypeIdModel> questionTypeIdModelList = questionTypeService.queryQuestionType(courseId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionTypeIdModelList);
    }


}    
